package com.fishery.order.controller;

import com.fishery.entity.Result;
import com.fishery.order.service.OrderService;
import com.fishery.order.service.client.BaseClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "溯源信息接口")
@CrossOrigin
@RestController
@RequestMapping("/traceability")
public class TraceabilityController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private BaseClient baseClient;

	@GetMapping("/info/{productId}/{sourceType}")
	@ApiOperation("溯源信息")
	public Result traceProcessingFactory(@PathVariable(name = "productId", required = true)
										 @ApiParam(value = "产品ID", required = true) String productId,
										 @PathVariable(name = "sourceType", required = true)
										 @ApiParam(value = "来源类型", required = true) String sourceType) {
		if (StringUtils.isEmpty(productId) || StringUtils.isEmpty(sourceType)) {
			return Result.error("溯源失败");
		}
		return orderService.traceInformationByProductIdAndSourceType(productId, sourceType);
	}


	@GetMapping("/product/{traceId}")
	@ApiOperation("通过溯源码获取所有要溯源的类型和产品id")
	public Result searchSourceTypeAndProductIdById(@PathVariable(name = "traceId", required = true)
												   @ApiParam(value = "溯源id", required = true) String traceId) {
		if (StringUtils.isEmpty(traceId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(orderService.getSourceTypeAndProductIdById(traceId));
	}

	@GetMapping("/feeding/more/{farmingId}/{size}/{current}/{catchTime}")
	@ApiOperation("溯源更多的喂养信息")
	public Result traceMoreFeedingInformationOfProduct(@PathVariable(name = "farmingId", required = true)
													   @ApiParam(value = "养殖编号", required = true) String farmingId,
													   @PathVariable(name = "size", required = true)
													   @ApiParam(value = "尺寸", required = true) int size,
													   @PathVariable(name = "current", required = true)
													   @ApiParam(value = "页码", required = true) int current,
													   @PathVariable(name = "catchTime", required = true)
													   @ApiParam(value = "捕捞时间", required = true) String catchTime) {
		if (StringUtils.isEmpty(farmingId)) {
			return Result.error("溯源失败");
		}
		return baseClient.traceMoreFeedingInformationOfProduct(farmingId, current, size, catchTime);
	}

	@GetMapping("/supply/{supplyId}")
	@ApiOperation("溯源投入品信息")
	public Result traceSupplyInformation(@PathVariable(name = "supplyId", required = true)
										 @ApiParam(value = "投入品id", required = true) String supplyId) {
		if (StringUtils.isEmpty(supplyId)) {
			return Result.error("溯源失败");
		}
		return Result.success().data(baseClient.traceSupplyInformation(supplyId));
	}
}
